package com.example.Project.user;

import com.example.Project.Class.MassOrganizations;
import com.example.Project.Class.User;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

public class UserController {
    private static User user;
    public ContextMenu menu;

    void start() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(LoginController.class.getResource("user.fxml"));
        Scene scene = new Scene((Parent)fxmlLoader.load(), 836.0, 526.0);
        Stage stage = new Stage();
        stage.setTitle("社团信息管理");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }
    void get(User auser){
        user=auser;
    }
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField accountContent;

    @FXML
    private TextField nameContent;
    @FXML
    private Pagination Page;

    @FXML
    private Button lookMassButton;
    @FXML
    private Button masterLookButton;

    @FXML
    private TextField phoneContent;

    @FXML
    private TextField sexContent;
    @FXML
    private void btnAdd(ActionEvent actionEvent)  {
        boolean re=userMethods.userSendAdd(user.getId(),table.getSelectionModel().getSelectedItem().getId());
        Alert al = new Alert(Alert.AlertType.CONFIRMATION);
        if(re){
            al.setHeaderText("申请已发送，请耐心等待结果！！！");
        }else {
            al.setHeaderText("申请失败，请不要重复申请，若非重复申请失败，请重新申请！！！");
        }
        al.showAndWait();
    }

    @FXML
    void lookMyMass(ActionEvent event) throws IOException {
        LookMyMassController enter=new LookMyMassController();
        System.out.println(user.getId());
        enter.get(user.getId());
        enter.start();
    }
    @FXML
    void initialize() throws IOException {
        if (user != null) {
            accountContent.setText(String.valueOf(user.getId()));
            accountContent.setEditable(false);
            nameContent.setText(user.getName());
            nameContent.setEditable(false);
            sexContent.setText(user.getSex());
            sexContent.setEditable(false);
            phoneContent.setText(user.getPhone());
            phoneContent.setEditable(false);
        }
        getPage();
        table.getItems().clear();
        ObservableList<MassOrganizations> massOrganizationsList= userMethods.getMassOrganization(page);
        System.out.println(massOrganizationsList);
        Platform.runLater(() -> updateTableView(massOrganizationsList));
    }
    @FXML
    void masterlook(ActionEvent event) throws IOException {
        ShowMasterMassController enter=new ShowMasterMassController();
        System.out.println(user.getId());
        enter.get(user.getId());
        enter.start();
    }
    @FXML
    private TableView<MassOrganizations> table;
    @FXML
    private TableColumn<MassOrganizations, Integer> tableId;
    @FXML
    private TableColumn<MassOrganizations, String> tableName;
    @FXML
    private TableColumn<MassOrganizations, Date> tableBirthday;
    @FXML
    private TableColumn<MassOrganizations, String> tableType;
    @FXML
    private TableColumn<MassOrganizations, String> tablePhone;
    com.example.Project.user.userHttpMethods.userMethods userMethods=new userMethods();
    private int page=1;
    private int totalPage=1;
    private void updateTableView(List<MassOrganizations> MassOrganizations) {
        this.table.getItems().addAll(MassOrganizations);
        this.tableId.setCellValueFactory(new PropertyValueFactory<>("Id"));
        this.tableName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        this.tableBirthday.setCellValueFactory(new PropertyValueFactory<>("Birthday"));
        this.tableType.setCellValueFactory(new PropertyValueFactory<>("Type"));
        this.tablePhone.setCellValueFactory(new PropertyValueFactory<>("Phone"));
    }
    public void getPage(){
        totalPage = userMethods.getTotalPage();
        System.out.println(totalPage);
        Page.setPageCount(totalPage);
        // 设置事件监听器，用于在页索引更改时触发查询操作
        Page.currentPageIndexProperty().addListener((observable, oldValue, newValue) -> {
            page = newValue.intValue()+1;
            System.out.println("page:"+page);
            newTable();
        });
    }
    private void newTable(){
        try {
            table.getItems().clear();
            ObservableList<MassOrganizations> updatedData = userMethods.getMassOrganization(page);
            Platform.runLater(() -> updateTableView(updatedData));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

